package com.example.zwh.computershop.order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zwh.computershop.FragmentAdapter;
import com.example.zwh.computershop.R;
import com.example.zwh.computershop.androidUtil.ActivityCollector;

import java.util.ArrayList;
import java.util.List;

public class OrderListActivity extends FragmentActivity implements View.OnClickListener{

    private ViewPager orderViewPager;
    private FragmentAdapter orderFragmentAdapter;

    ImageView order_back;
    TextView allOrder, waitngPayOrder, waitingReceiveOrder, waitingCommentOrder;
    AllOrderFragment allOrderFragment;

    WaitingCommentFragment waitingCommentFragment;
    WaitingPayFragment waitingPayFragment;
    WaitingReceiveFragment waitingReceiveFragment;
    List<Fragment> fragList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list);
        ActivityCollector.addActivity(this);
        initView();
        initEvent();
    }
//接收过来的数据
    public static void actionStart(Context context, int position) {
        Intent intent = new Intent(context, OrderListActivity.class);
        intent.putExtra("position", position);
        context.startActivity(intent);
    }

    void initView() {
        orderViewPager = findViewById(R.id.order_viewpager);

        order_back = findViewById(R.id.orderlist_back);
        allOrder = findViewById(R.id.all_order);
        waitngPayOrder = findViewById(R.id.waiting_pay_order);
        waitingReceiveOrder = findViewById(R.id.waiting_receive);
        waitingCommentOrder = findViewById(R.id.waiting_comment);
        //全部订单，待评价订单，待收货订单，待支付订单
        allOrderFragment = new AllOrderFragment();
        waitingCommentFragment = new WaitingCommentFragment();
        waitingReceiveFragment = new WaitingReceiveFragment();
        waitingPayFragment = new WaitingPayFragment();

        fragList.add(allOrderFragment);
        fragList.add(waitingCommentFragment);
        fragList.add(waitingPayFragment);
        fragList.add(waitingReceiveFragment);

        orderFragmentAdapter = new FragmentAdapter(this.getSupportFragmentManager(), fragList);
        //为ViewPager设置adapter
        orderViewPager.setAdapter(orderFragmentAdapter);

        int position = getIntent().getIntExtra("position", 0);
        orderViewPager.setCurrentItem(position);
        onItemSeleted(position);
    }

    void initEvent() {
        order_back.setOnClickListener(this);
        allOrder.setOnClickListener(this);
        waitngPayOrder.setOnClickListener(this);
        waitingReceiveOrder.setOnClickListener(this);
        waitingCommentOrder.setOnClickListener(this);

        orderViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int i) {
                int currentItem = orderViewPager.getCurrentItem();
                switch (currentItem) {
                    case 0:
                        //allOrder.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
                        onItemSeleted(i);
                        break;
                    case 1:
                        onItemSeleted(i);
                        break;
                    case 2:
                        onItemSeleted(i);
                        break;
                    case 3:
                        onItemSeleted(i);
                         break;
                    default:
                        break;
                }
            }


            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }
            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    private void onItemSeleted(int item){
        resetItemColor();
        switch (item){
            case 0:
                allOrder.setSelected(true);
                break;
            case 1:
                waitngPayOrder.setSelected(true);
                break;
            case 2:
                waitingReceiveOrder.setSelected(true);
                break;
            case 3:
                waitingCommentOrder.setSelected(true);
                break;


        }
    }
    //置为灰色
    private void resetItemColor() {
        allOrder.setSelected(false);
        waitingReceiveOrder.setSelected(false);
        waitingCommentOrder.setSelected(false);
        waitngPayOrder.setSelected(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.orderlist_back :
                finish();
                break;
            case R.id.all_order :
                orderViewPager.setCurrentItem(0);
                onItemSeleted(0);
                Toast.makeText(this, "所有订单", Toast.LENGTH_SHORT).show();
                break;
            case R.id.waiting_pay_order:
                orderViewPager.setCurrentItem(1);
                onItemSeleted(1);
                Toast.makeText(this, "待付款", Toast.LENGTH_SHORT).show();
                break;
            case R.id.waiting_receive:
                orderViewPager.setCurrentItem(2);
                onItemSeleted(2);
                Toast.makeText(this, "待收货", Toast.LENGTH_SHORT).show();
                break;
            case R.id.waiting_comment :
                orderViewPager.setCurrentItem(3);
                onItemSeleted(3);
                Toast.makeText(this, "待评论", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
